/**
 * 计算并返回图片缩放后的参数
 * @param maxWidth 最大宽度
 * @param maxHeight 最大高度
 * @param width 照片实际宽度
 * @param height 照片实际高度
 */
function clacImgZoomParam(maxWidth, maxHeight, width, height) {
    var param = {top:0, left:0, width:width, height:height};
    if(width>maxWidth || height>maxHeight ) {
        rateWidth = width / maxWidth;
        rateHeight = height / maxHeight;
        if( rateWidth > rateHeight ) {
            param.width = maxWidth;
            param.height = Math.round(height / rateWidth);
        } else {
            param.width = Math.round(width / rateHeight);
            param.height = maxHeight;
        }
    }
    param.left = Math.round((maxWidth - param.width) / 2);
    param.top = Math.round((maxHeight - param.height) / 2);
    return param;
}

/**
 * 等比例压缩图片
 * @param maxWidth 最大宽度
 * @param maxHeight 最大高度
 * @param imgObj 需要缩放的img对象
 */
function resizePreview(maxWidth, maxHeight, imgObj) {
    var newImg = new Image();
    newImg.src = imgObj.src;
    var para = clacImgZoomParam(maxWidth, maxHeight, (newImg.width?newImg.width:imgObj.offsetWidth),
            (newImg.height?newImg.height:imgObj.offsetHeight));
    imgObj.style.width = para.width+'px';
    imgObj.style.height = para.height+'px';
    imgObj.style.marginLeft = para.left+'px';
    imgObj.style.marginTop = para.top+'px';
}


/**
 * 图片上传时的预览：图片预览区需放在一个id为 "preview"+imgIndex 的div内，该div内部有一个img元素。
 *  【为兼容IE6\7\8】且需设置class为imgView的img的样式为：
 * <br/><i>.imgView{filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);}</i>
 * 
 * @param maxWidth 最大宽度
 * @param maxHeight 最大高度
 * @param file 上传图片的file控件
 * @param imgIndex 需要缩放的img对象
 */
function previewImage(maxWidth, maxHeight, file, imgIndex) {
    if(!file.value.match(/.jpg|.gif|.png|.bmp/i)){
        alert('图片格式无效！');
        return false;
    }

    var MAXWIDTH = maxWidth;
    var MAXHEIGHT = maxHeight;
    var div = document.getElementById('preview'+imgIndex);
    if (file.files && file.files[0]) { //非IE6、7、8
        div.innerHTML = '<img id="imghead'+imgIndex+'" class="imgView" />';
        var img = document.getElementById('imghead'+imgIndex);
        img.onload = function(){
            var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
            img.style.width = rect.width+'px';
            img.style.height = rect.height+'px';
            img.style.marginLeft = rect.left+'px';
            img.style.marginTop = rect.top+'px';
        }
        var reader = new FileReader();
        reader.onload = function(evt){img.src = evt.target.result;}
        reader.readAsDataURL(file.files[0]);
    } else { //IE6、7、8
        var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
        file.select();
        window.top.document.body.focus();
        var src = document.selection.createRange().text;
        div.innerHTML = '<img id="imghead'+imgIndex+'" class="imgView">';
        var img = document.getElementById('imghead'+imgIndex);
        img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        var rect = clacImgZoomParam(MAXWIDTH-2, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
        status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
        div.innerHTML = "<div id='showImgDiv' style='width:"+rect.width+"px;height:"+rect.height+"px;margin-top:"+rect.top+"px;margin-left:"+rect.left+"px;"+sFilter+src+"\"'></div>";
    }
}